<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="加价名称" prop="markupsName" label-width="68px">
        <el-input
          z
          v-model="queryParams.markupsName"
          placeholder="请输入加价名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计价类型" prop="markupsType">
        <el-select v-model="queryParams.markupsType">
          <el-option :value="null" label="全部"></el-option>
          <el-option value="一口价" label="一口价"></el-option>
          <el-option value="单价" label="单价"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['service/config-center:markups:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="markupsList"
      height="490"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" prop="id" label="ID" />
      <el-table-column
        label="加价名称"
        align="center"
        prop="markupsName"
        width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="起始方量" align="center" prop="startingVolumes" />
      <el-table-column label="结束方量" align="center" prop="endingVolumes" />
      <el-table-column label="计价类型" align="center" prop="markupsType" />
      <el-table-column label="价格" align="center" prop="markupsValue" />
      <el-table-column
        label="最后编辑时间"
        align="center"
        prop="updateTime"
        min-width="160"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ formatDate(scope.row.updateTime) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="enable">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enable" disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['service/config-center:markups:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['service/config-center:markups:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改服务加价配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="加价名称" prop="markupsName">
          <el-input v-model="form.markupsName" placeholder="请输入加价名称" />
        </el-form-item>
        <el-form-item label="起始方量" prop="startingVolumes">
          <div style="display: flex">
            <el-checkbox
              v-model="startingVolumesCheck"
              style="margin-right: 22px"
              @change="handleStartingVolumesCheck"
              >0方</el-checkbox
            >
            <el-input
              v-model="form.startingVolumes"
              :disabled="startingVolumesCheck"
              placeholder="请输入起始方量"
            />
          </div>
        </el-form-item>
        <el-form-item label="结束方量" prop="endingVolumes">
          <div style="display: flex">
            <el-checkbox
              v-model="endingVolumesCheck"
              style="margin-right: 18px"
              @change="handleEndingVolumesCheck"
              >不限</el-checkbox
            >
            <el-input
              v-model="form.endingVolumes"
              :disabled="endingVolumesCheck"
              placeholder="请输入结束方量"
            />
          </div>
        </el-form-item>
        <el-form-item label="计价类型" prop="markupsType">
          <el-radio-group v-model="form.markupsType">
            <el-radio label="一口价">一口价</el-radio>
            <el-radio label="单价">单价</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="加价值"
          prop="markupsValue"
          v-if="form.markupsType"
        >
          <el-input v-model="form.markupsValue" placeholder="请输入加价值">
            <template slot="append">{{
              form.markupsType === "一口价" ? "元" : "元/方"
            }}</template>
          </el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.enable"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMarkups,
  getMarkups,
  delMarkups,
  addMarkups,
  updateMarkups,
} from "@/api/service/config-center/markups.js";
import { formatDate } from "@/utils";
export default {
  name: "Markups",
  data() {
    return {
      startingVolumesCheck: false,
      endingVolumesCheck: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务加价配置表格数据
      markupsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        markupsName: null,
        markupsType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        markupsName: [
          { required: true, message: "加价名称不能为空", trigger: "blur" },
        ],
        startingVolumes: [
          {
            required: true,
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (parseFloat(value).toString() == "NaN") {
                callback("请输入数字");
              } else {
                callback();
              }
            },
          },
        ],
        endingVolumes: [
          {
            required: true,
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (parseFloat(value).toString() == "NaN") {
                callback("请输入数字");
              } else {
                callback();
              }
            },
          },
        ],
        markupsType: [
          { required: true, message: "加价类型不能为空", trigger: "change" },
        ],
        markupsValue: [
          {
            required: true,
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (parseFloat(value).toString() == "NaN") {
                callback("请输入数字");
              } else {
                callback();
              }
            },
          },
        ],
        enable: [
          { required: true, message: "是否启用不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatDate,
    handleStartingVolumesCheck() {
      this.form.startingVolumes = 0;
    },
    handleEndingVolumesCheck() {
      this.form.endingVolumes = -1;
    },
    /** 查询服务加价配置列表 */
    getList() {
      this.loading = true;
      listMarkups(this.queryParams).then((response) => {
        this.markupsList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        markupsName: null,
        startingVolumes: null,
        endingVolumes: null,
        markupsType: null,
        markupsValue: null,
        enable: null,
        updateBy: null,
        updateTime: null,
      };
      this.startingVolumesCheck = false;
      this.endingVolumesCheck = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增服务加价";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改服务加价";
      if (row.endingVolumes === 0) {
        this.form.endingVolumesCheck = true;
      }
      if (row.startingVolumes === -1) {
        this.form.startingVolumes = true;
      }
      this.form = { ...this.form, ...row };
      // const id = row.id || this.ids;
      // getMarkups(id).then((response) => {
      //   this.form = response.data;

      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMarkups(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMarkups(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除服务加价配置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMarkups(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "service/config-center/markups/export",
        {
          ...this.queryParams,
        },
        `markups_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
